package com.zoweunion.mechanic.plugins.mq;


import com.alibaba.fastjson.JSON;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.mq.RabbitmqUtil;
import com.zoweunion.mechanic.util.task.MQDataAlarmPush;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitMQCustomer {

    private final Logger logger = LoggerFactory.getLogger(RabbitMQCustomer.class);

    @Autowired
    private MQDataAlarmPush mqDataAlarmPush;
    @Autowired
    private RabbitmqUtil rabbitmqUtil;

    /**
     * 实时报文数据，做数据业务逻辑处理
     * @param message
     * @throws
     */
    @RabbitListener(queues = "real_remind_alarm_message")
    public void channelRealDataMessageHandler(String message) {
        try{
            mqDataAlarmPush.channelRealRemindAlarm(message);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
    public void operationLogMq(Integer operation_model, Integer operation_type, String user_id, String user_name,
                               String operated_person_id, String operated_person_name, String car_id, String car_name, String s_id, String s_name, String remark) {
        Map<String, Object> operationLog = new HashMap<>();
        operationLog.put("operation_model", operation_model);
        operationLog.put("operation_type", operation_type);
        operationLog.put("user_id", user_id);
        operationLog.put("user_name", user_name);
        operationLog.put("operated_person_id", operated_person_id);
        operationLog.put("operated_person_name", operated_person_name);
        operationLog.put("car_id", car_id);
        operationLog.put("car_name", car_name);
        operationLog.put("s_id", s_id);
        operationLog.put("s_name", s_name);
        operationLog.put("remark", remark);
        operationLog.put("log_type", Constants.LogType.getLogTypeCode("操作"));
        operationLog.put("create_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        rabbitmqUtil.sendLogServerMessage(JSON.toJSONString(operationLog));
    }

    public void operationLoginMq(Map<String, Object> operationLog) {
        operationLog.put("log_type", Constants.LogType.getLogTypeCode("登录"));
        operationLog.put("create_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        rabbitmqUtil.sendLogServerMessage(JSON.toJSONString(operationLog));
    }
}
